////////////////////////////////////////////////
// DESCRIPTION:
//    Support for the iMXS PWM
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using Microsoft.SPOT;
using FusionWare.SPOT.Runtime.Serialization;

namespace FusionWare.SPOT.EmulatorComponents.iMXS
{
    /// <summary>Provides an emulatoed PWM</summary>
    /// <remarks>
    /// Derived classes *MUST* not rely on the base
    /// class to always be ComPortComponent. As this is
    /// likely to change once a real interop solution
    /// exists from MS. It is reasonably safe to assume
    /// that all future versions will be derived directly
    /// or indirectly from EmulatorComponent.
    /// </remarks>
    class PWM : ComPortComponent
    {
        public PWM()
            : base((int)VPortIDs.VPORT_DEV_PWM, 1, 9)
        {
        }

        /// <summary>Data structure containing data sent to the HAL from the managed application</summary>
        [Serializable]
        public class PWMData
        {
            /// <summary>Requested Frequency [valid only if Enable is true]</summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields" )]
            public uint Frequency;

            /// <summary>Duty cycle in percentage (0-100) [valid only if Enable is true]</summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields" )]
            public uint Duty;

            // Note: placement of this last is important
            // for alignment issues in the native code
            /// <summary>true if PWM should be enabled; false if it should be disabled</summary>
            [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields" )]
            public bool Enable;

            /// <summary>Constructs a new PWM data instance</summary>
            internal PWMData()
            {
                this.Frequency = 0;
                this.Duty = 0;
                this.Enable = false;
            }
        }

        /// <summary>handles received data from the </summary>
        protected override void OnDataReceived()
        {
            PWMData data = new PWMData( );
            BinarySerializer.DeSerialize(data, base.DeviceViewStream );            
            System.Diagnostics.Debug.Print("Emulated PWM:");
            System.Diagnostics.Debug.Print("\tFrequency: {0}", data.Frequency);
            System.Diagnostics.Debug.Print("\t     Duty: {0}", data.Duty);
            System.Diagnostics.Debug.Print("\t   Enable: {0}", data.Enable);
            base.DeviceViewStream.Flush();
            OnPWMData( data );
        }

        /// <summary>Handles PWM Data received from the Managed code application</summary>
        /// <param name="RxData">PWM data from the calling application</param>
        /// <remarks>
        /// The base implementation does nothing. This is designed to allow derived
        /// classes the ability to emulate behaviour without worrying about how
        /// the data gets into the component. 
        /// </remarks>
        protected virtual void OnPWMData( PWMData RxData )
        {
        }
    }
}
